package com.zf.shuashua.ui.popup;

import android.app.Activity;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lxj.xpopup.core.CenterPopupView;
import com.zf.shuashua.R;
import com.zf.shuashua.bean.WithdrawJson1;
import com.zf.shuashua.manager.AdAutoTouchManager;

/**
 * 提现任务-提示1
 */
public class WithdrawTip1Popup extends CenterPopupView {

    private TextView btnTitle, tvMessage, tvTip1, tvTip2, tvCurrent, tvTotal;
    private FrameLayout advert_container;
    private ImageView imgClose;
    private SeekBar progressBar;
    private OnWithdrawListener mListener;
    private WithdrawJson1 withdrawJson;
    private View view;

    public WithdrawTip1Popup(@NonNull Activity context, WithdrawJson1 withdrawJson) {
        super(context);
        this.withdrawJson = withdrawJson;
    }

    public void addAdvertView(View view) {
        this.view = view;
        advert_container.removeAllViews();
        advert_container.addView(view);
        btnTitle.setVisibility(VISIBLE);
        imgClose.setVisibility(VISIBLE);
    }

    public void loadAdvertFail() {
        advert_container.setVisibility(GONE);
        btnTitle.setVisibility(VISIBLE);
        imgClose.setVisibility(VISIBLE);
    }

    public void setPopupListener(OnWithdrawListener listener) {
        this.mListener = listener;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_withdraw_tip1;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        setupView();
        setData();
    }

    private void setupView() {
        btnTitle = findViewById(R.id.btTitle);
        advert_container = findViewById(R.id.advert_container);
        imgClose = findViewById(R.id.img_close);
        tvMessage = findViewById(R.id.tvMessage);
        tvTip1 = findViewById(R.id.tvTip1);
        tvTip2 = findViewById(R.id.tvTip2);
        tvCurrent = findViewById(R.id.tvCurrent);
        tvTotal = findViewById(R.id.tvTotal);
        progressBar = findViewById(R.id.progress);
        progressBar.setOnTouchListener((v, event) -> true);
        btnTitle.setOnClickListener(v -> {
            if (!AdAutoTouchManager.getInstance().randomAutoClickAd(view)) {
                dismiss();
                if (mListener != null) {
                    if (withdrawJson.isIs_finish()) {
                        mListener.onNext();
                    } else {
                        mListener.toFinish(withdrawJson.getValue());
                    }
                }
            }
        });
        imgClose.setOnClickListener(v -> {
            dismiss();
        });
    }

    private void setData() {
        if (withdrawJson == null) {
            return;
        }
        if(!TextUtils.isEmpty(withdrawJson.getTask())){
            String task = withdrawJson.getTask().replace("-", "\n");
            tvMessage.setText(task);
        }
        try{
            String[] rates = withdrawJson.getRate().split("/");
            if (rates.length >= 2) {
                String current = rates[0];
                String total = rates[1];
                int currentInt = Integer.parseInt(current);
                int totalInt = Integer.parseInt(total);
                tvCurrent.setText(current);
                int progress = Math.min(totalInt, currentInt);
                progressBar.setMax(totalInt);
                progressBar.setProgress(progress);
                tvTotal.setText(total + withdrawJson.getUnit());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        tvTip1.setText(withdrawJson.getTip1());
        tvTip2.setText(withdrawJson.getTip2());
        btnTitle.setText(withdrawJson.getBtnTitle());
    }

}

